package com.wl.eb.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wl.eb.module.EbComment;
import com.wl.eb.service.CommentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;

/**
 * @Description: 用一句话描述
 * @Auther: 新梦想*陈超
 * @Date: 2021/12/6 16:23
 */
@Controller//证
@RequestMapping("/comment")// 工号
public class CommentController {
    @Resource
    private CommentService commentService;

    /**
     * 分页查询最新的留言信息列表
     * @param model 绑定数据的model
     * @param page  页数
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model,
        @RequestParam(value = "page",defaultValue = "1") int page){
        System.out.println("进入CommentController的list方法，page:"+page);
        //1.调用service分页查询留言列表
        Page commentPage = commentService.list(page);
        //2.绑定数据到model
        model.addAttribute("commentPage",commentPage);
        //3.返回view
        return "guestbook";//templates/guestbook.html
    }

    /**
     * 添加留言
     * @param ebComment   前端传过来的留言信息
     * @return
     */
    @PostMapping
    public String save(EbComment ebComment) {
        System.out.println("进入CommentController的save,ebComment："+ebComment);
        //1.调用service添加留言信息
        boolean result = commentService.save(ebComment);
        if(!result){
            throw  new RuntimeException("添加留言失败！！");
        }
        //2.绑定数据到model---不需要返回数据

        //3.返回view
        return "redirect:comment/list";//跳转http://localhost:8080/comment/list
    }

}
